package com.hly.product.controller;

import com.hly.common.utils.PageUtils;
import com.hly.common.utils.R;
import com.hly.product.entity.AttrGroupEntity;
import com.hly.product.service.AttrGroupService;
import com.hly.product.service.CategoryService;
import com.hly.product.vo.EditableTabs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author dudulu
 * @email 52039@Miku.com
 * @date 2021-03-27 12:08:09
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据分类id获取所有分组下的所有规格属性
     */
    @RequestMapping("editableTabsList/list")
    public R EditableTabsListAll(@RequestParam Map<String, Object> params){
        List<EditableTabs> editableTabsList = attrGroupService.getEditableTabsList(params);

        return R.ok().put("list", editableTabsList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{cateGoryId}")
    public R attrGroupList(
            @PathVariable("cateGoryId")Long cateGoryId,
            @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryAttrGroupPage(cateGoryId , params);

        return R.ok().put("page", page);
    }

    /**
     *  根据id 、关联表获取关联属性信息
     */
    @GetMapping("/asslist")
    public R AssociateAttrList(  @RequestParam HashMap<String,Object> params ) {

        return R.ok().put("list", attrGroupService.AssAttrPage(params));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long cateLogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCateLogPath(cateLogId);
        attrGroup.setCateLogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeAssByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
